package com.liuyoung.clockview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

/**
 * Created by Administrator on 2017/2/22.
 */

public class ClockView extends View {

    private Paint mPaint;

    private Bitmap mDialBitmap;
    private Bitmap mSecondBitmap;
    private Bitmap mMinuteBitmap;
    private Bitmap mHourBitmap;

    private int mDialWidth;
    private int mDialHeight;

    Context mContext;

    Matrix matrix;

    float scaleWidth,scaleHeight;

    Calendar c;

    public ClockView(Context context) {
        super(context);
    }

    public ClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initAttrs(attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿

    }

    private void initAttrs(AttributeSet attrs) {
        if (attrs != null){
            Log.d("abc","attr");
            TypedArray array = null;//返回一个由AttributeSet获得的一系列的基本的属性值
            try{
                array = getContext().obtainStyledAttributes(attrs,R.styleable.ClockView);

                mHourBitmap = BitmapFactory.decodeResource(getResources(),
                        array.getResourceId(R.styleable.ClockView_hourHand,0));

                mMinuteBitmap = BitmapFactory.decodeResource(getResources(),
                        array.getResourceId(R.styleable.ClockView_minuteHand,0));
                
                mSecondBitmap = BitmapFactory.decodeResource(getResources(),
                        array.getResourceId(R.styleable.ClockView_secondHand,0));

                mDialBitmap = BitmapFactory.decodeResource(getResources(),
                        array.getResourceId(R.styleable.ClockView_dial,0));

                mesaureBitmap();
                
            }finally {
                if (array != null){
                    array.recycle();
                }
            }
        }
    }

    private void mesaureBitmap() {

        if (mDialBitmap == null){
            throw new RuntimeException("drawable 不能为空");
        }
        mDialWidth =  mDialBitmap.getWidth();
        mDialHeight = mDialBitmap.getHeight();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("abc","onMeasure");
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode,width),measureHeight(heightMode,height));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mDialBitmap,0,0,mPaint);
        canvas.save();

        c = Calendar.getInstance();

        int hour = c.get(Calendar.HOUR);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        float hourRotate = hour* 30.0f + minute / 60.0f * 30.0f +180;

        float minuteRotate = minute* 6.0f +180;

        float secondRotate = second *6.0f+180;

        canvas.rotate(hourRotate ,mDialWidth/2, mDialHeight/2);
        canvas.drawBitmap(mHourBitmap ,
                0,0,
                mPaint);
        canvas.restore();
        canvas.save();//出了一个bitmap，进行一些操作，固定住，别影响后续控件添加

        canvas.rotate(minuteRotate ,mDialWidth/2, mDialHeight/2);
        canvas.drawBitmap(mMinuteBitmap,
                0,0,
                mPaint);
        canvas.restore();
        canvas.save();

        canvas.rotate(secondRotate ,mDialWidth/2, mDialHeight/2);
        canvas.drawBitmap(mSecondBitmap,
                0,0,
                mPaint);
        canvas.save();

        this.postInvalidateDelayed(1000);

    }

    private int measureWidth(int mode,int width){
        switch (mode){
            case MeasureSpec.UNSPECIFIED:  Log.d("abc","UNSPECIFIED");
            case MeasureSpec.AT_MOST:Log.d("abc","AT_MOST");break;
            case MeasureSpec.EXACTLY:
                Log.d("abc","EXACTLY");
                mDialWidth = width;
                Log.d("abc","mDialWidth "+mDialWidth);
                Log.d("abc","mDialHeight "+mDialHeight);
                mDialBitmap = Bitmap.createScaledBitmap(mDialBitmap,mDialWidth,mDialHeight,true);
                mHourBitmap = Bitmap.createScaledBitmap(mHourBitmap,mDialWidth,mDialHeight,true);
                mMinuteBitmap = Bitmap.createScaledBitmap(mMinuteBitmap,mDialWidth,mDialHeight,true);
                mSecondBitmap = Bitmap.createScaledBitmap(mSecondBitmap,mDialWidth,mDialHeight,true);
                break;
        }
        return mDialWidth;
    }

    private int measureHeight(int mode,int height){
        switch (mode){
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:break;
            case MeasureSpec.EXACTLY:
                mDialHeight = height;break;
        }
        return mDialHeight;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //?
        //这里是否应该回收，不可见 onDraw 也停止了绘制，是否有回收必要？
    }
}